package interface_layer;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import database_layer.Common;
import database_layer.UpdateDispatcher;

/**
 * As it's named, this servlet handles student course registrations/un-registrations
 * in the web service. It receives it's information from the corresponding JSP,
 * and communicates with the DB.
 */
public class CourseRegistration extends HttpServlet
{
	private static final long serialVersionUID = 1L;
       
    public CourseRegistration()
    {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
	{
		try
		{
			String operation = request.getParameter("operation");
			String courseName = request.getParameter("course");
			String username = (String) request.getSession().getAttribute("username");
			int returnValue = Common.NO_ERROR;
			
			if (operation.equals("Register"))
			{
				returnValue = UpdateDispatcher.registerUserToCourse(username, courseName);
				ErrorHandler.parseForErrors(returnValue, response, "courseDetails.jsp?course=" + courseName);
			}
			
			else if (operation.equals("Unregister"))
			{
				returnValue = UpdateDispatcher.unregisterUserFromCourse(username, courseName);
				ErrorHandler.parseForErrors(returnValue, response, "courseDetails.jsp?course=" + courseName);
			}
			
			else
			{
				ErrorHandler.parseForErrors(Common.ILLEGAL_OPERATION, response, null);
			}
		}
		catch (Exception e)
		{
			System.out.println("Exception is : " + e.getLocalizedMessage());
			ErrorHandler.parseForErrors(Common.INTERNAL_ERROR, response, null);
		}
	}

}
